package com.codeoss.framework.websocket.biz;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;

import org.springframework.stereotype.Component;

import com.codeoss.framework.response.ViewResponse;
import com.codeoss.framework.view.renderer.ViewPropMap;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

@ServerEndpoint(value = "/websocket/test/one")
@Component
public class OneWebSocket {
	/** 记录当前在线连接数 */
	private static AtomicInteger onlineCount = new AtomicInteger(0);

	private static Map<String,Session> sessionMap=new  HashMap<String,Session>();
	
	public OneWebSocket() {
		ObjectMapper objectMapper = new ObjectMapper();
		final Thread t = new Thread() {

			public void run() {
				while (true) {
					try {
						sleep(5000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					if(sessionMap.size()>0) {
						String id = "test_dashboard_component7";
						List<Map<String, Object>> dataList = new ArrayList<Map<String, Object>>();
						Map<String, Object> map = new HashMap<String, Object>();
						dataList.add(map);
						map.put("sex_count", Math.random());

						ViewResponse viewResponse=new ViewResponse();
						ViewPropMap viewPropMap = new ViewPropMap();
						viewResponse.setViewPropMap(viewPropMap);
						Map<String, Object> map2 = new HashMap<String, Object>();
						map2.put("data", dataList);
						viewPropMap.put(id, map2);
						
						Iterator<Map.Entry<String, Session>> iterator=sessionMap.entrySet().iterator();
						while(iterator.hasNext()) {
							Session session = (Session) iterator.next().getValue();
							try {
								String s = objectMapper.writeValueAsString(viewResponse);
								session.getBasicRemote().sendText(s);
							} catch (JsonProcessingException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							} catch (Exception e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}
					}
				}
			}
		};
		t.start();
	}
	
	/**
	 * 连接建立成功调用的方法
	 */
	@OnOpen
	public void onOpen(Session session) {
		sessionMap.put(session.getId(),session);
		onlineCount.incrementAndGet(); // 在线数加1
		System.out.println("Open" + session.getId() + "::" + onlineCount.get());
		System.out.println(session.getRequestParameterMap().get("X-Token"));
		
	}

	/**
	 * 连接关闭调用的方法
	 */
	@OnClose
	public void onClose(Session session) {
		onlineCount.decrementAndGet(); // 在线数减1
		System.out.println("Close" + session.getId() + "::" + onlineCount.get());
		sessionMap.remove(session.getId());
	}

	/**
	 * 收到客户端消息后调用的方法
	 *
	 * @param message 客户端发送过来的消息
	 */
	@OnMessage
	public void onMessage(String message, Session session) {
		System.out.println("Message" + session.getId() + "::" + message);
	}

	@OnError
	public void onError(Session session, Throwable error) {
		error.printStackTrace();
	}
}
